package com.dindang.ddm.mvp.ui.adapter.multitype;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.TextView;

import com.dindang.ddm.R;
import com.dindang.ddm.mvp.model.entity.Backlog;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class BacklogViewBinder extends ItemViewBinder<Backlog.ToDo.Rows, BacklogViewBinder.ViewHolder> {

    OnClickListener listener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_backlog, parent, false);
        return new ViewHolder(root);
    }

    public BacklogViewBinder setListener(OnClickListener listener){
        this.listener = listener;
        return this;
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Backlog.ToDo.Rows backlog) {
        switch (backlog.c_status) {
            case "1":
                holder.btn_progress.setVisibility(View.VISIBLE);
                holder.btn_undo.setVisibility(View.VISIBLE);
                holder.btn_order.setVisibility(View.GONE);
                holder.btn_plan.setVisibility(View.GONE);
                holder.btn_complete.setVisibility(View.GONE);
                holder.btn_pause.setVisibility(View.GONE);
                holder.btn_continue.setVisibility(View.GONE);
                holder.btn_send_single.setVisibility(View.VISIBLE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
            case "2":
                holder.btn_progress.setVisibility(View.GONE);
                holder.btn_undo.setVisibility(View.GONE);
                holder.btn_order.setVisibility(View.VISIBLE);
                holder.btn_plan.setVisibility(View.GONE);
                holder.btn_complete.setVisibility(View.GONE);
                holder.btn_pause.setVisibility(View.GONE);
                holder.btn_continue.setVisibility(View.GONE);
                holder.btn_send_single.setVisibility(View.GONE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
            case "3":
                holder.btn_progress.setVisibility(View.VISIBLE);
                holder.btn_undo.setVisibility(View.GONE);
                holder.btn_order.setVisibility(View.GONE);
                holder.btn_plan.setVisibility(View.VISIBLE);
                holder.btn_complete.setVisibility(View.VISIBLE);
                holder.btn_pause.setVisibility(View.VISIBLE);
                holder.btn_continue.setVisibility(View.GONE);
                holder.btn_send_single.setVisibility(View.GONE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
            case "4":
                holder.btn_progress.setVisibility(View.GONE);
                holder.btn_undo.setVisibility(View.GONE);
                holder.btn_order.setVisibility(View.GONE);
                holder.btn_plan.setVisibility(View.GONE);
                holder.btn_complete.setVisibility(View.GONE);
                holder.btn_pause.setVisibility(View.GONE);
                holder.btn_continue.setVisibility(View.VISIBLE);
                holder.btn_send_single.setVisibility(View.GONE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
            case "5":
                holder.btn_progress.setVisibility(View.GONE);
                holder.btn_undo.setVisibility(View.GONE);
                holder.btn_order.setVisibility(View.GONE);
                holder.btn_plan.setVisibility(View.GONE);
                holder.btn_complete.setVisibility(View.GONE);
                holder.btn_pause.setVisibility(View.GONE);
                holder.btn_continue.setVisibility(View.GONE);
                holder.btn_send_single.setVisibility(View.GONE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
            default:
                holder.btn_progress.setVisibility(View.GONE);
                holder.btn_undo.setVisibility(View.GONE);
                holder.btn_order.setVisibility(View.GONE);
                holder.btn_plan.setVisibility(View.GONE);
                holder.btn_complete.setVisibility(View.GONE);
                holder.btn_pause.setVisibility(View.GONE);
                holder.btn_continue.setVisibility(View.GONE);
                holder.btn_send_single.setVisibility(View.GONE);
                holder.btn_return_visit.setVisibility(View.GONE);
                break;
        }

        if("1".equals(backlog.c_applyArea)){
            holder.tv_repair_area.setText("私有区域");
        }else if("2".equals(backlog.c_applyArea)){
            holder.tv_repair_area.setText("公共区域");
        }

        holder.tv_repair_personnel.setText(backlog.c_applyName);
        holder.tv_fault_types.setText(backlog.c_errtype + "-" + backlog.c_subtype);
        holder.tv_fault_address.setText(backlog.c_address);
        if("".equals(backlog.flow_appoint_date)){
            holder.tv_time_hint.setText("预约时间: ");
            holder.tv_arrival_time.setText(backlog.d_appointdate + " " + backlog.c_start_date+"至"+backlog.c_end_date);
        }else{
            holder.tv_time_hint.setText("到场时间: ");
            holder.tv_arrival_time.setText(backlog.d_appointdate + " " + backlog.flow_start_date+"至"+backlog.flow_end_date);
        }

        holder.tv_village_name.setText(backlog.c_buildingname);
        holder.tv_timedate.setText(backlog.d_createdate);
        holder.tv_date.setText(backlog.d_createdate);

        holder.cl_backlog.setOnClickListener(v -> listener.onClick(holder.getAdapterPosition()));
        holder.btn_progress.setOnClickListener(v -> listener.onProgress(holder.getAdapterPosition()));
        holder.btn_complete.setOnClickListener(v -> listener.onComplete(holder.getAdapterPosition()));
        holder.btn_plan.setOnClickListener(v -> listener.onPlan(holder.getAdapterPosition()));
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout cl_backlog;

        TextView tv_timedate;

        TextView tv_repair_area;
        TextView tv_repair_personnel;
        TextView tv_fault_types;
        TextView tv_fault_address;
        TextView tv_time_hint;
        TextView tv_arrival_time;
        TextView tv_village_name;
        TextView tv_date;

        TextView btn_progress;
        TextView btn_undo;
        TextView btn_order;
        TextView btn_plan;
        TextView btn_complete;
        TextView btn_pause;
        TextView btn_continue;
        TextView btn_send_single;
        TextView btn_return_visit;
        ViewHolder(View itemView) {
            super(itemView);
            cl_backlog = itemView.findViewById(R.id.cl_backlog);

            tv_timedate = itemView.findViewById(R.id.tv_timedate);

            tv_repair_area = itemView.findViewById(R.id.tv_repair_area);
            tv_repair_personnel = itemView.findViewById(R.id.tv_repair_personnel);
            tv_fault_types = itemView.findViewById(R.id.tv_fault_types);
            tv_fault_address = itemView.findViewById(R.id.tv_fault_address);
            tv_time_hint = itemView.findViewById(R.id.tv_time_hint);
            tv_arrival_time = itemView.findViewById(R.id.tv_arrival_time);
            tv_village_name = itemView.findViewById(R.id.tv_village_name);
            tv_date = itemView.findViewById(R.id.tv_date);

            btn_progress = itemView.findViewById(R.id.btn_progress);
            btn_undo = itemView.findViewById(R.id.btn_undo);
            btn_order = itemView.findViewById(R.id.btn_order);
            btn_plan = itemView.findViewById(R.id.btn_plan);
            btn_complete = itemView.findViewById(R.id.btn_complete);
            btn_pause = itemView.findViewById(R.id.btn_pause);
            btn_continue = itemView.findViewById(R.id.btn_continue);
            btn_send_single = itemView.findViewById(R.id.btn_send_single);
            btn_return_visit = itemView.findViewById(R.id.btn_return_visit);
        }
    }

    public interface OnClickListener {
        void onClick(int position);// 点击跳转
        void onProgress(int position);// 追记
        void onUndo(int position);// 撤销
        void onOrder(int position);// 接单
        void onPlan(int position);// 方案
        void onComplete(int position);// 完成
        void onPause(int position);// 暂停
        void onContinue(int position);// 继续
        void onSendOrders(int position);// 派单
    }
}
